﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;

public class AddPrefabConnectWindow : EditorWindow
{

    [MenuItem("Window/AddPrefabConnectWindow")]  //���Ӳ˵�ѡ��
    public static void ShowWindow()
    {
        var window = EditorWindow.GetWindow<AddPrefabConnectWindow>();
        window.Show();
    }
    private GameObject connectPrefab;
    private void OnGUI()
    {
        EditorGUILayout.LabelField("关联预设体");
        connectPrefab = (GameObject)EditorGUILayout.ObjectField(connectPrefab, typeof(GameObject));
        if (GUILayout.Button("确认关联") && connectPrefab != null)
        {
            // bool isPrefab = PrefabUtility.IsAnyPrefabInstanceRoot(connectPrefab);
            // if (!isPrefab)
            // {
            //     Debug.LogError($"{connectPrefab.name}非预设体");
            //     return;
            // }
            var path = AssetDatabase.GetAssetPath(connectPrefab);
            var objs = Selection.gameObjects;
            if (objs != null)
            {
                var listVector = new List<object[]>();
                foreach (var obj in objs)
                {
                    var position = obj.transform.localPosition;
                    var rotation = obj.transform.localRotation;
                    var scale = obj.transform.localScale;
                    listVector.Add(new object[] { position, rotation, scale });
                    PrefabUtility.SaveAsPrefabAssetAndConnect(obj, path, InteractionMode.UserAction);

                }
                for (int i = 0; i < listVector.Count; i++)
                {
                    var obj = objs[i];
                    obj.transform.localPosition = (Vector3)listVector[i][0];
                    obj.transform.localRotation = (Quaternion)listVector[i][1];
                    obj.transform.localScale = (Vector3)listVector[i][2];
                }
            }
        }
        if (GUILayout.Button("清理missing脚本组件"))
        {
            this.ClearMissingComponent();
        }
    }
    public void ClearMissingComponent()
    {
        var scene = SceneManager.GetActiveScene();
        foreach (var gameobject in scene.GetRootGameObjects())
        {
            RecursiveObject(gameobject);
            // RecursiveObject(gameobject, (g) =>
            // {
            //     GameObjectUtility.RemoveMonoBehavioursWithMissingScript(gameobject);
            // });
        }
        AssetDatabase.SaveAssets();
    }
    public static void RecursiveObject(GameObject gameObject)
    {
        GameObjectUtility.RemoveMonoBehavioursWithMissingScript(gameObject);
        EditorUtility.SetDirty(gameObject);
        //recursive?.Invoke(gameObject);
        for (int i = 0; i < gameObject.transform.childCount; i++)
        {

            RecursiveObject(gameObject.transform.GetChild(i).gameObject);
            //RecursiveObject(gameObject.transform.GetChild(i).gameObject, recursive);
        }
    }
}